package com.google.appengine.codelab;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Classe com funcionalidades comuns para os desenvolvedores do SCAF
 * 
 * @author
 * 
 */
public class Common {

	/**
	 * Conversor de String para Data
	 * 
	 */
	public static Date cDate(String ref) {
		try {

			try {
				DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
				Date result = df.parse(ref);
				return result;
			} catch (Exception e) {

			}

			try {
				DateFormat df = new SimpleDateFormat("yyyyMMdd");
				Date result = df.parse(ref);
				return result;
			} catch (Exception e) {
				throw new RuntimeException("Data Invalida.", e);
			}

		} catch (Exception e) {
			throw new RuntimeException("Data Invalida.", e);
		}

	}

	public static String dateToString(Date ref) {
		return new SimpleDateFormat("dd/MM/yyyy").format(ref);
	}

	public static String intToDate(String data) {
		try {
			return dateToString(new SimpleDateFormat("yyyyMMdd").parse(data
					));
		} catch (ParseException e) {
			throw new RuntimeException(e.getMessage(), e);
		}
	}

	public static Integer dateToInt(Date data) {
		try {
			return Integer.parseInt(new SimpleDateFormat("yyyyMMdd")
					.format(data));
		} catch (Exception e) {
			throw new RuntimeException("Data Inv��lida.", e);
		}
	}

	public static Integer dateToInt(String data) {
		return dateToInt(cDate(data));
	}

	/**
	 * Conversor de String para Data e Hora
	 * 
	 */
	public static Date cDateHora(String ref) {
		try {
			DateFormat df = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
			Date result = df.parse(ref);
			return result;

		} catch (Exception e) {
			throw new RuntimeException("Data Inv��lida.", e);
		}
	}

	/**
	 * Conversor de String para Int
	 * 
	 */
	public static int cInt(String ref) {
		try {
			int result = Integer.parseInt(ref);
			return result;
		} catch (Exception e) {
			return (Integer) null;
		}
	}

	/**
	 * Conversor de int, dbl para string
	 * 
	 */
	public static String cStr(Object object) {
		return String.valueOf(object);
	}

	/**
	 * Conversor de String para Double
	 * 
	 */
	public static Double cDbl(String valor) {
		if (valor == null || valor.isEmpty()) {
			return null;
		}
		return Double.parseDouble(valor.replaceAll("\\.", "").replaceAll(",",
				"."));
	}

	public static BigDecimal stringToBigdecimal(String valor) {
		if (valor == null || valor.isEmpty()) {
			return null;
		}
		return new BigDecimal(valor.replaceAll("\\.", "").replaceAll(",", "."));
	}

	/**
	 * Data Hoje
	 * 
	 */
	public static String now() {
		Date date = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
		return dateFormat.format(date);
	}

	/**
	 * Data Add
	 * 
	 */
	public static String dateAdd(Date dateRef, int days) {
		Calendar cal = Calendar.getInstance();
		SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
		cal.setTime(dateRef);
		cal.add(Calendar.DATE, days);
		return dateFormat.format(cal.getTime());
	}

	/**
	 * Data Add
	 * 
	 */
	public static String dateAdd(String dateRef, int days) {
		return dateAdd(cDate(dateRef), days);
	}

	/*
	 * Gera numero randomico entre max e min
	 */
	public static int random(int max, int min) {
		return min + (int) (Math.random() * max);
	}

	public static String StrDateToDataScaf(String DataRecebida)
			{ // Feita por Guilherme Lopes
		try{
			DateFormat FormatoRecebido = new SimpleDateFormat("dd/mm/yyyy");
			DateFormat FormatoScaf = new SimpleDateFormat("yyyymmdd");
			Date datan = FormatoRecebido.parse(DataRecebida);
	
			String DataConvertida = FormatoScaf.format(datan);
	
			return DataConvertida;
		}
		catch( Exception e ){
			throw new RuntimeException(e.getMessage(),e);
		}
	}

}
